home *** CD-ROM | disk | FTP | other *** search
- Path: news.rrz.uni-hamburg.de!rzdspc1!simmons
- From: simmons@rzdspc1.informatik.uni-hamburg.de (Geoffrey Simmons)
- Newsgroups: comp.lang.c,comp.unix.programmer,comp.databases
- Subject: Re: Aligning struct fields with NDBM (was: Casting unsigned short ...)
- Date: 9 Jan 96 10:46:17 GMT
- Organization: University of Hamburg -- Germany
- Message-ID: <simmons.821184377@rzdspc1>
- References: <simmons.820857453@rzdspc1> <simmons.821116795@rzdspc1> <821142071snz@genesis.demon.co.uk>
- NNTP-Posting-Host: rzdspc1.informatik.uni-hamburg.de
-
- Lawrence Kirby <fred@genesis.demon.co.uk> writes:
-
- >While ndbm isn't part of the C language, your problem seems very much C
- >language related so I've reinstated comp.lang.c.
-
- OK. (I try to be careful about littering newsgroups, esp. since I'm not a
- regular around here.)
-
- I wrote:
- >> if ((MyRec = (RecType *) malloc(theDatum.dsize)) == NULL)
-
- and then:
- >> MyRec = (RecType *) theDatum.dptr;
-
- Lawrence gently points out the obvious:
- >You've just gone and thrown away the pointer to your malloc'd block (this is
- >a memory leak). Maybe you meant to copy the data in the struct in which case
- >you need instead:
-
- > memcpy(MyRec, theDatum.dptr, sizeof(RecType));
-
- >Don't use:
-
- > *MyRec = *(RecType *) theDatum.dptr;
-
- >because this may assume alignment that is not guaranteed.
-
- This was my problem, and using memcpy() is the right solution. I must say,
- when my mistake is pointed out to me, I find my own silliness breathtaking.
- Thanks also to Chris Dodd <dodd@csl.sri.com> for pointing out this error
- as well by e-mail.
-
- >>Is this a well-known problem with NDBM, with a well-known solution?
-
- >There is nothing in your post that indicates a problem with ndbm, you
- >just need to fix your code.
-
- I didn't mean to accuse NDBM; but I can imagine that others who have used
- it have had the same difficulties with memory alignment that I had.
-
- While I'm here, do you know of a good way to use the debugger to get at the
- bottom of a problem like this (alignment errors, and possibly memory leaks)?
- I struggled with this mainly because the debugger (GNU GDB 4.15.1) could
- do everything that the running program couldn't do, so Usenet became my
- only hope.
-
- Eureka!
- Geoff
- --
- Geoffrey Simmons | simmons@informatik.uni-hamburg.de | "Insert wise
- University of Hamburg | Phone: (++49 40) 54715-381 | and witty
- Vogt-Koelln-Str. 30 | Fax: (++49 40) 54715-385 | quotation
- D-22527 Hamburg, Germany | | here."
-